require(["../../conf/config.js"],function(){
	require(["jquery","common","ajaxMapping","template"],function($,common,ajaxMapping,template){
		//判断洋码头的登录状态
		var loginStatus = 0;
		var userObj = null;
		var cookiestr = getCookie("userlist");
		var userArray = JSON.parse(cookiestr);
		(function judge(){
			userArray.forEach(function(item){
				if(item.status == 1){
					loginStatus = 1;
					userObj = item;
					$(".username").html(userObj.username);
					$(".login").css("display","none");
					$(".register").css("display","none");
				}
			})
		})();
		
		
		//鼠标划入用户名
		var username = document.getElementsByClassName("username")[0];
		$(".username").on("mouseenter",function(){
			var span = document.createElement("span");
			span.innerText = "退出登录";
			span.className = "tuichu";
			username.appendChild(span);
			console.log($(".tuichu"));
			//*****鼠标点击退出登录*********
			span.addEventListener("click",function(){
				console.log("aaa");
				userArray.forEach(function(item){
						item.status = 0;		
				})
				setCookie("userlist", JSON.stringify(userArray), 20);
				loginStatus = 0;
				window.location.replace("http://localhost:8000/pages/index/index.html")
			})
		})
		
		//鼠标划出用户名
		$(".username").on("mouseleave",function(){
			var tuichu = document.getElementsByClassName("tuichu")[0];
			tuichu.remove();
			console.log("aaa");
		})
		
		//购物车渲染
		$("#proContain").load("car.html",function(){
			if(userObj.shopcar.length == 0){
				empty();
			}else{
				 htmlstr = template('temp01',{list:userObj.shopcar});
				// ????用jquery的html()方法会把script标签也读取出来
				document.getElementById("proContain").innerHTML = htmlstr;
			}
		
		})
		
		//点击logo回到主页
		$("#header").click(function(){
				window.location.replace("http://localhost:8000/pages/index/index.html")
			})
		//为解决异步 
	   setTimeout(function(){
		   //处理总金额
		   var allmoney = Array.from(document.getElementsByClassName("allmoney"));
		   allmoney.forEach(function(item){
		   	item.innerHTML = Number(item.previousElementSibling.innerHTML)*Number((item.previousElementSibling.previousElementSibling.innerHTML).split("￥")[1]);
		   });
		   //购物车减法
		   var relbtn = Array.from(document.getElementsByTagName("button"));
		   relbtn.forEach(function(item){
			   item.onclick = function(){
				   var til = this.parentNode.parentNode.children[0].innerHTML;
				   userObj.shopcar.forEach(function(item,index){
					   if(item.carttitle == til){
							userObj.shopcar.splice(index,1)
					   }
				   });
				   userArray.forEach(function(item){
						if(item.phone = userObj.phone){
							item = userObj;
						}
				   });  
				    setCookie("userlist",JSON.stringify(userArray),20);
				    this.parentNode.parentNode.remove();	
					if(userObj.shopcar.length == 0){
						window.location.reload();
					}
			   }
		   });
		   	   
  
	   },50)
	   function empty(){
			   document.getElementById("proContain").innerHTML=
			   `
				<div id="empty">
					<img src="../../static/images/empty.jpg" >
					<p>您的购物车空空荡荡......<a href="../index/index.html">去逛逛?</a></p>
				</div>
			   `
	   };
//***********************************************************************	
	})
})